#ifndef CCACHE_RENDERER_H
#define CCACHE_RENDERER_H

#include <SDL.h>
#include <vector>
#include <set>

#include "CSpriteManager.h"
#include "CRenderer.h"

class CCacheSprite
{
public:
	size_t num;
	size_t x;
	size_t y;
	SpriteType t;

	CCacheSprite(SpriteType t_, size_t x_, size_t y_, size_t num_)
		: t(t_)
		, x(x_)
		, y(y_)
		, num(num_)
	{
	}

	CCacheSprite(const CCacheSprite& rhs)
		: t(rhs.t)
		, x(rhs.x)
		, y(rhs.y)
		, num(rhs.num)
	{
	}

	bool operator ==(const CCacheSprite& rhs) const
	{
		return cmp(rhs);
	}

	bool operator !=(const CCacheSprite& rhs) const
	{
		return !cmp(rhs);
	}

	bool cmp(const CCacheSprite& rhs) const
	{
		return (t == rhs.t &&
			x == rhs.x &&
			y == rhs.y);
	}
};




struct CDiffSprite : public CCacheSprite
{
	CDiffSprite(const CCacheSprite& s, bool a)
		: CCacheSprite(s)
		, add(a)
	{
	}

	bool add;
};

class CCacheRenderer : public CRenderer
{
public:
	CCacheRenderer(SDL_Surface* screen
		, CSpriteManager* sprite_manager
		, const size_t screenw
		, const size_t screenh
		, const size_t top_left_corner_map_x
		, const size_t top_left_corner_map_y)
		: CRenderer(screen, 
		sprite_manager, 
		screenw, screenh,
		top_left_corner_map_x, top_left_corner_map_y)
	{
	}

	virtual void DrawSprite(SpriteType t, size_t x, size_t y);
	virtual void Redraw();
private:
	typedef std::vector<CCacheSprite> sprites_container;
	typedef std::vector<CDiffSprite> diff_container;

	void RealDrawSprite(SpriteType t, size_t x, size_t y);
	void RealDrawSprite(SpriteType t, size_t x, size_t y, SDL_Rect& dst_rect);

	void PrintDiff(const int* c, size_t i, size_t j, size_t m, size_t n);
	void Diff();

	
	sprites_container SpritesOnScreen;
	sprites_container SpritesOnPool;
	diff_container DiffSprites;

	size_t LastNum;
};

#endif // CCACHE_RENDERER_H